import { _decorator, Node } from 'cc';
import { PanelBase } from '../../Basic/UI/PanelBase';
import { LayerType } from '../../Basic/Config/BaseEnum';
import { SettlementModel } from './SettlementModel';
import { GameModel } from '../Game/GameModel';
import { LevelModel } from '../Level/LevelModel';
import { MultiPlatform } from '../../Basic/Model/Platform/MultiPlatform';
import { VideoEvent } from '../../Data/Types';
import { EventManager } from '../../Basic/Manager/EventManager';
import { EventType } from '../Config/GameEnum';
import { GEEvent, GravityModel } from '../../Basic/Model/SDK/GravityModel';
const { ccclass, property } = _decorator;

@ccclass('SettlementPanel')
export class SettlementPanel extends PanelBase {
  panelLayer: LayerType.PanelLayer = LayerType.PanelLayer;
  @property(Node)
  winNode: Node;
  @property(Node)
  loseNode: Node;
  @property(Node)
  btn_revive: Node;
  @property(Node)
  btn_again: Node;
  // @property(Node)
  // step_lab: Node;
  override blackMask: number = 0;
  override clickBackClose: boolean = false;

  protected onEnable(): void {
    super.onEnable();
    EventManager.Ins().register(EventType.AD_REVIVE, this.onAd, this);
    this.refreshUI();
  }

  protected onDisable(): void {
    EventManager.Ins().unRegister(EventType.AD_REVIVE, this.onAd);
  }

  refreshUI() {
    this.winNode.active = SettlementModel.Ins().isPass;
    this.loseNode.active = !this.winNode.active;
    this.btn_revive.active = GameModel.Ins().reviveCount > 0;
    this.btn_again.active = GameModel.Ins().reviveCount <= 0;
    // this.step_lab.active = GameModel.Ins().curGridCount >= 0;
  }

  onAd() {
    this.clickClose();
    GameModel.Ins().cellMachine.revive();
  }

  onClickBtn(t: Touch, data) {
    const key = data.toString();
    switch (key) {
      case 'back':
        this.clickClose();
        GameModel.Ins().basePanel.clickClose();
        LevelModel.Ins().Show();
        break;
      case 'next':
      case 'again':
        this.clickClose();
        GameModel.Ins().basePanel.clickClose();
        GameModel.Ins().Show();
        break;
      case 'revive':
        MultiPlatform.Ins().platformUnit.showVideo(VideoEvent.Revive);
        // GravityModel.I.track(GEEvent.AD_SHOW, { adPosition: `看广告复活` });
        break;
    }
  }
}
